package com.thundersoft.pickcolor.activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.thundersoft.pickcolor.R;
import com.thundersoft.pickcolor.database.DatabaseHelper;
import com.thundersoft.pickcolor.utils.MD5Util;
import com.thundersoft.pickcolor.utils.ToastUtils;

/**
 * 注册
 */

public class RegisterActivity extends AppCompatActivity {

    private EditText et_register_username,et_register_password;
    private DatabaseHelper mDatabaseHelper;
    private SQLiteDatabase mSQLiteDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        // 显示返回键
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        et_register_username = findViewById(R.id.et_register_username);
        et_register_password = findViewById(R.id.et_register_password);

        mDatabaseHelper = new DatabaseHelper(this,"PickColor.db",
                null,1);

    }

    public void toRegister(View view) {
        String username = et_register_username.getText().toString().trim();
        String password = et_register_password.getText().toString().trim();
        password = MD5Util.encrypt(password);
        if(querySQLite(username)){
            // 用户名不存在
            if(insertSQLite(username,password)){
                // 插入成功
                ToastUtils.Success(this,getString(R.string.register_success));
                finish();
            }else {
                ToastUtils.Error(this,getString(R.string.register_failed));
            }
        }else {
            ToastUtils.Info(this,getString(R.string.register_user_exist));
        }
    }

    private boolean insertSQLite(String username, String password) {
        try {
            mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name",username);
            values.put("password",password);
            mSQLiteDatabase.insert("User",null,values);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 查询数据库是否有该用户
     * @param username
     * @return
     */
    private boolean querySQLite(String username) {
        mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
        Cursor cursor = null;
        try {
            cursor = mSQLiteDatabase.query("User",null,"name = ?",
                    new String[]{username},null,null,null);
            if(cursor.moveToFirst()){
                // 数据库有该用户，不可以注册
                return false;
            }else {
                // 数据库没有该用户，可以注册
                return true;
            }
        }catch (Exception e){
            return false;
        }finally {
            cursor.close();
        }
    }

    // 菜单栏操作
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
